﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Dycox.Drawing;

namespace Dycox.Reporting
{
    public abstract class BlockElement : ReportElement
    {
        //protected internal override System.Drawing.RectangleF GetBounds(ReportRenderer renderer)
        //{
        //    return _bounds;
        //}

        //private Bounds _bounds;
        //[XmlAttribute("bounds"), TypeConverter(typeof(BoundsConverter))]
        //public Bounds Bounds
        //{
        //    get { return _bounds; }
        //    set { _bounds = value; }
        //}



        public PointF AbsoluteLocation
        {
            get
            {
                ContainerElement container = this.Parent;
                PointF point = _location;

                while (container != null)
                {
                    PointF location = container.Location;

                    point.X += location.X;
                    point.Y += location.Y;

                    container = container.Parent;
                }

                return _location;
            }
        }


        private SizeF? _size;

        [XmlAttribute("size")]
        public SizeF Size
        {
            get { return _size.GetValueOrDefault(); }
            set { _size = value; }
        }

        private PointF _location;
        [XmlAttribute("location"), TypeConverter(typeof(PointFConverter))]
        public PointF Location
        {
            get { return _location; }
            set { _location = value; }
        }

        private Paddings _paddings;

        public Paddings Paddings
        {
            get { return _paddings; }
            set { _paddings = value; }
        }


        public bool AutoSize
        {
            get
            {
                return !_size.HasValue;
            }
        }

        protected internal abstract SizeF GetClientSize(RenderingContext context);

        protected internal override RectangleF GetBounds(RenderingContext context)
        {
            SizeF size;

            if (AutoSize)
            {
                size = GetClientSize(context);
                var paddings = this.Paddings;

                size = new SizeF(size.Width + paddings.Left + paddings.Right, size.Height + paddings.Top + paddings.Bottom);
            }
            else
                size = _size.Value;

            return new RectangleF(_location, size);
        }


    }
}
